package middle.ground.http.interceptor;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * RestTemplate的拦截器，可以在这里实现批量为每一个RestTemplate的请求添加统一的请求头
 *
 * @author jrl
 * @date 2020/2/23 09:53
 */
@Component
public class RestTemplateInterceptor implements ClientHttpRequestInterceptor {

	@Override
	public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
		HttpHeaders headers = request.getHeaders();
		headers.add("ContentType", MediaType.APPLICATION_JSON_UTF8_VALUE);
		// 加入请求头
		// headers.add("token", token);

		return execution.execute(request, body);
	}

}
